package org.hot100_5.leetcode22;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    List<String> res = new ArrayList<>();
    StringBuilder item = new StringBuilder();
    public List<String> generateParenthesis(int n) {
        backtracking(n,0,0);
        return res;
    }

    private void backtracking(int n,int leftCount,int rightCount){
        if (item.length()==n*2){
            res.add(new String(item));
            return;
        }

        if (leftCount<n){
            item.append("(");
            leftCount++;
            backtracking(n,leftCount,rightCount);
            item.deleteCharAt(item.length()-1);
            leftCount--;
        }

        if (rightCount<leftCount){
            item.append(")");
            rightCount++;
            backtracking(n,leftCount,rightCount);
            item.deleteCharAt(item.length()-1);
            rightCount--;
        }

    }
}
